/**
 * Created by Administrator on 2016/10/20.
 */
function updatefields(url,div,fields,data_id,fun){
    $(document).on('click',div, function () {
        var ids=$(this).attr(data_id);
        var text=$(this).text();
        $(this).parent().html('<input type="text" class="layui-input" style="width: 100px;" value="'+text+'" data_c="updatefield"/>');
        var field=$('[data_c="updatefield"]');
        field.focus();
        field.blur(function(){
            var values=$(this).val();
            //{table:table,id:id,field:field,value:value}
            $.post(url,{id:ids,values:values,fields:fields,type:'field'},function(result){
                fun(result);
            },'json');
            return false;
        })
    });
}

//api启用
$.ajaxApiConfirm=function(options,datafun){
    var defaults = {
        layer:false,
        url:false,
        other:false,
        data_id:0, //属性
        status:0,
        confirmMsg:'确认操作',
    };
    $.extend(defaults,options);
    var layer=defaults.layer;
    var data_id=defaults.data_id;
    var other=defaults.other;
    var status=defaults.status;
    layer.confirm(defaults.confirmMsg, {icon: 3, title:'提示'}, function(index){
        $.ajaxApiUrl({
            layer:layer,
            url:defaults.url,
            data:{id:data_id,other:other,status:status},
            async:false,
            is_load:false,
            datafun:function(data){
                datafun(data);
                layer.close(index);
            }
        });
    })

};

//api启用插件写法
$.fn.ajaxApiStatus=function(options,datafun){
    var defaults = {
        layer:false,
        url:false,
        data_status:'data_status', //属性
        data_id:'data_id', //属性
        message_before:'启用',
        message_after:'关闭'
    };
    $.extend(defaults,options);
    var layer=defaults.layer;
    $(this).click(function(){
        var data_id=$(this).attr(defaults.data_id);
        var data_status=$(this).attr(defaults.data_status);
        var title=data_status==0?defaults.message_before:defaults.message_after;
        var confirmMsg='确定'+title+'?';
        layer.confirm(confirmMsg, {icon: 3, title:'提示'}, function(index){
            $.ajaxApiUrl({
                layer:layer,
                url:defaults.url,
                data:{id:data_id,status:data_status},
                async:false,
                is_load:false,
                datafun:function(data){
                    datafun(data);
                    layer.close(index);
                }
            });
        })
    });

};

//api快速搜索
$.fn.ajaxApiMemberInfo=function(Url){
    if(!Url){
        Url='/index.php/common/MemberInfoHeader';
    }
    $(this).click(function(){
        $.layer_type_2({
            layer:layer,
            title:'快速搜索',
            area:['700px','620px'],
            offset:'5%',
            url:Url
        })
    });
};
(function(){
    $.extend({
        layer_allandchilchecked:function(options){
            var defaults = {
                layui_form:false,
                allchecked:'allchecked',
                checked:'checked',
            };
            $.extend(defaults,options);

            var layui_form=defaults.layui_form;
            var allchecked=defaults.allchecked;
            var checked=defaults.checked;

            //全选
            layui_form.on('checkbox('+allchecked+')', function(data){
                allcheckfun(allchecked,data.elem.checked,data.elem);
            });
            layui_form.on('checkbox('+checked+')', function(data){
                allcheckfun(checked,data.elem.checked,data.elem);
            });
            allcheckfun(allchecked);
            function allcheckfun(click_elem,elem_checked,checkedDiv){

                // console.log(childDiv)
                //click_elem被点击对象
                if(click_elem==allchecked){
                    var childDiv=$(checkedDiv).parent().parent().find('[data_c=child]');
                    if(elem_checked){
                        childDiv.find('[data_c="'+checked+'"]').prop('checked',true);
                        childDiv.prop('checked',true);
                    }else{
                        childDiv.find('[data_c="'+checked+'"]').prop('checked',false);
                        childDiv.prop('checked',false);
                    }
                }else{

                    var childDiv=$(checkedDiv).parent().parent().parent().parent().find('[data_c=child]');
                    var parentDiv=$(checkedDiv).parent().parent().parent().parent().find('.title');
                    var _checked=0;
                    childDiv.find('[data_c="'+checked+'"]').each(function(){
                        if($(this).is(':checked')){
                            _checked++;
                        }
                    });
                    if(_checked>0){
                        parentDiv.find('[data_c="'+allchecked+'"]').prop('checked',true);
                    }else{
                        parentDiv.find('[data_c="'+allchecked+'"]').prop('checked',false);
                    }
                }
                layui_form.render();
            }
        }
    })
})(jQuery);

$.ajaxHighcharts = function(options){
    var defaults = {
        data:{},
        SelectedDiv:false,
        type:'spline',
        title:false,
        subtitle:false,
        x_data:false,
        x_title:false,
        y_data:false,
        y_title:false,
        key_name:false,
        height:false,
    };
    $.extend(defaults,options);
    var data=defaults.data;
    if(!data){
        alert('请传数据');
        return false
    }
    $(defaults.SelectedDiv).highcharts({
        chart: {
            height:defaults.height,
            type: defaults.type
        },
        title: {
            text: defaults.title
        },
        subtitle: {
            text: defaults.subtitle
        },
        xAxis: {
            categories: defaults.x_data,
            title: {
                text: defaults.x_title
            }
        },
        yAxis: {
            title: {
                text: defaults.y_title
            }
        },
        plotOptions: {
            line: {
                dataLabels: {
                    enabled: true          // 开启数据标签
                },
                enableMouseTracking: true // 关闭鼠标跟踪，对应的提示框、点击事件会失效
            }
        },
        series: [{
            name: defaults.key_name,
            data: defaults.data
        }],
        legend: {
            enabled: false
        },
        credits: {
            enabled: false
        },
        exporting: {
            enabled: false
        }
    });

};

//打印
$.ajaxPrinter = function(options){
    console.log('开始打印');
    var defaults = {
        page_count:1,
        page_size:80,
        printer_id:0,
        title:'小票打印',
        strHTML:false,
        LODOP:false,
        time:false
    };
    $.extend(defaults,options);
    var data=defaults.strHTML;
    var LODOP=defaults.LODOP;
    if(!data){
        alert('请传数据');
        return false
    }
    if(!LODOP.VERSION){
        alert('请检查打印机插件');
        return false
    }
    if(defaults.time){
        clearTimeout(defaults.time);
        console.log('清除循环');
        console.log(LODOP);
    }
    console.log(LODOP);
    LODOP.PRINT_INIT(defaults.title);

    var strStyleCSS='<link href="/Public/css/xiaopiao.css" type="text/css" rel="stylesheet">';
    strStyleCSS+='<link href="/Public/css/membercard.css" type="text/css" rel="stylesheet">';
    strStyleCSS+= '<link href="/Public/css/fapiao.css" type="text/css" rel="stylesheet">';

    var strFormHtml="<head>"+strStyleCSS+"</head><body>"+data+"</body>";
//                    LODOP.PRINT_INITA(10,20,810,610,"");
    LODOP.SET_PRINT_COPIES(defaults.page_count);//指定份数
    LODOP.SET_PRINTER_INDEXA(defaults.printer_id);
    LODOP.SET_PRINT_PAGESIZE(3,defaults.page_size*10,5,'');//这里3表示纵向打印且纸高“按内容的高度”；1385表示纸宽138.5mm；5表示页底空白0.5mm
    LODOP.SET_PRINT_STYLEA(0,"ItemType",4);
    LODOP.NewPageA();
    LODOP.ADD_PRINT_HTM(0,0,defaults.page_size*10,"100%",strFormHtml);
    if (LODOP.CVERSION) {
        LODOP.On_Return_Remain=true;
        LODOP.On_Return=function(TaskID,Value){
            if (TaskID==TaskID){
                console.log('打印成功');
            };
        };
        // TaskID=LODOP.PRINTA();
        // LODOP.PREVIEW();
        LODOP.PRINT();
    } else {
        // TaskID=LODOP.PRINTA();
        // LODOP.PREVIEW();
        LODOP.PRINT();
    };

};



$(document).on("mouseover mouseout",'[data_c="ZSeeContent"]',function(event){
    if(event.type == "mouseover"){
        $(this).find('[data_c="HideZSeeContent"]').show();
    }else if(event.type == "mouseout"){
        $(this).find('[data_c="HideZSeeContent"]').hide();
    }
});
